package com.zh.ssmshirofreemarker.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.zh.ssmshirofreemarker.cache.ShiroCache;
import com.zh.ssmshirofreemarker.cache.ShiroCacheManager;
import com.zh.ssmshirofreemarker.entity.Permission;
import com.zh.ssmshirofreemarker.mapper.PermissionMapper;
import com.zh.ssmshirofreemarker.service.IPermissionService;
import com.zh.ssmshirofreemarker.service.RedisService;
import com.zh.ssmshirofreemarker.shiro.MyShiroRealm;
import com.zh.ssmshirofreemarker.websocket.WebSocketTest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.EncodeException;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
public class TestController {

    @Autowired
    RedisService redisService;

    @Autowired
    IPermissionService permissionService;

    @ResponseBody
    @RequestMapping("/remove")
    public String remove(){
        MyShiroRealm.clearCachedAuthorization();
        return "";
    }

    @ResponseBody
    @RequestMapping("/getMenu")
    public List<Map<String,Object>> getAllMenu(){
        return permissionService.getAllMenu();
    }

    @RequestMapping("/test")
    public String test(Map<String,Object> map){
        map.put("test","zh");
        return "test";
    }

    @Autowired
    PermissionMapper permissionMapper;

    @ResponseBody
    @RequestMapping("/getPermission")
    public Page<Permission> selectPermissionPage(Page<Permission> page,Permission permission){
        return page.setRecords(permissionMapper.selectPermissionList(page,permission));
    }

    @ResponseBody
    @RequestMapping("/sendMsg")
    public String sendMessage(String msg) throws IOException, EncodeException {
        WebSocketTest.sendMessage(msg,"zh");
        Long[] ids = new Long[2];
        permissionService.deleteBatchIds(Arrays.asList(ids));
        return "success";
    }



}
